<?php

/**
 * Description of G_Mail
 *
 * @author Monk <eoe2005@gmail.com>
 */
class G_Mail
{
    public static function init(){}
}
class GC_Mail{
    const CRLF = "\r\n";
    private $_host;
    private $_port;
    private $_protocol;
    private $_from;
    private $_to;
    private $_cc;
    private $_subject;
    private $_body;
    private $_charset;
    private $_attachment;
    private $_ishtml;
    private $_con;
    public function send(){
        if($this->_put("", 250)){
            if($this->_put('EHLO smtp.126.com', 250)){
                if($this->_put('AUTH LOGIN', 334)){
                    if($this->_put(base64_encode($this->_username), 334)){
                        if($this->_put(base64_encode($this->_userpass), 235)){
                            if($this->_put('MAIL FROM: '.$this->_from, 250)){
                                if($this->_to){
                                    foreach($this->_to AS $k => $v){
                                        $this->_put('RCPT TO: '.$k, 250);
                                    }
                                }
                                if($this->_cc){
                                    foreach($this->_cc AS $k => $v){
                                        $this->_put('RCPT TO: '.$k, 250);
                                    }
                                }
                                if($this->_put('DATA', 354)){
                                    //@to 构造结构体
                                    if($this->_put(".", 250)){
                                        return $this->_put('QUIT', 221);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }else{
            //连接失败
        }
        return false;
    }
    public function from($name,$mail){
        $this->_from = array($mail=>$name);
    }
    public function to($name,$mail){
        if($this->_to){
            $this->_to[$mail] = $name;
        }else{
            $this->_to = array($mail => $name);
        }
    }
    public function cc($name,$mail){
        if($this->_cc){
            $this->_cc[$mail] = $name;
        }else{
            $this->_cc = array($mail => $name);
        }
    }
    public function tos($users){
        foreach($users AS $name => $mail){
            $this->to($name, $mail);
        }
    }
    public function ccs($users){
        foreach($users AS $name => $mail){
            $this->cc($name, $mail);
        }
    }
    public function subject($title){
        $this->_subject = $title;
    }
    public function body($body){
        $this->_body = $body;
    }
    public function attachment($filename){
        if($this->_attachment){
            $this->_attachment[] = $filename;
        }else{
            $this->_attachment = array($filename);
        }
    }
    public function charset($charset = 'utf8'){
        $this->_charset = $charset;
    }
    public function setHtml(){
        $this->_ishtml = true;
    }
    public function setPlain(){
        $this->_ishtml = false;
    }
    /**
     * 发送数据并确定是否返回正确的数据
     * @param type $data
     * @param type $returncode
     */
    protected function _put($data,$returncode){
        if($data){
            $sendstr = $data.self::CRLF;
            @fwrite($this->_con,$sendstr , count($sendstr));
        }
        $getdata = "";
        while($line = fgets($this->_con)){
            $getdata = $line;
        }
        if($getdata){
            $len = count($getdata);
            if($len >= 3){
                return intval(substr($getdata, 0, 3)) == $returncode;
            }
            
        }
        return FALSE;
    }
}
